package cn.lbd.datastructure;


//二叉搜索树
public class BST {
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        int arr[] = new int[]{7, 3, 10, 12, 5, 1, 9, 2};
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(new BinarySearchTreeNode(arr[i]));
        }
        System.out.println("删除前");
        binarySearchTree.infixOrder();

        binarySearchTree.delNode(2);
        binarySearchTree.delNode(5);
        binarySearchTree.delNode(9);
        binarySearchTree.delNode(12);
        binarySearchTree.delNode(7);
        binarySearchTree.delNode(3);
        binarySearchTree.delNode(10);
        binarySearchTree.delNode(1);

        System.out.println("删除节点后");
        binarySearchTree.infixOrder();
    }
}


class BinarySearchTree {
    //根节点
    private BinarySearchTreeNode root;

    public BinarySearchTreeNode getRoot() {
        return this.root;
    }

    //插入
    public void add(BinarySearchTreeNode node) {
        if (root == null) {//树为空，则直接插入节点
            root = node;
        } else {//如果当前树不为空
            root.add(node);
        }
    }

    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("BST为空，无法遍历");
        }
    }

    //搜寻待删除结点
    public BinarySearchTreeNode search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //搜寻待删除结点的父节点
    public BinarySearchTreeNode searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //搜寻当前节点的右子树中最小的节点，并将其删除，返回这个被删除的结点的值
    public int delMinNode(BinarySearchTreeNode node) {
        BinarySearchTreeNode target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    //搜寻当前节点的左子树子树中最大的节点，并将其删除，返回这个被删除的结点的值
    public int delMaxNode(BinarySearchTreeNode node) {
        BinarySearchTreeNode target = node;
        while (target.right != null) {
            target = target.right;
        }
        delNode(target.value);
        return target.value;
    }


    //BST删除节点：1.叶子 2.一个子树 3.两个子树
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            BinarySearchTreeNode targetNode = search(value);
            //没有在树里找到待删除结点
            if (targetNode == null) {
                return;
            }
            //如果二叉排序树只有一个节点(根节点)
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            BinarySearchTreeNode parent = searchParent(value);
            //如果待删除结点是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                //如果是父节点的左子节点
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                    //如果是父节点的右子节点
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {//待删除结点有左右两个子树
                //搜寻当前节点的右子树中最小的节点，保存到临时变量并将其删除
                int minNodeIndex = delMinNode(targetNode.right);
                //将这个临时值替换待删除结点的值
                targetNode.value = minNodeIndex;

                /*int maxNodeIndex = delMaxNode(targetNode.left);
                targetNode.value = maxNodeIndex;*/
            } else {//待删除结点有一个子树（左或右）
                //待删除结点只有左子节点
                if (targetNode.left != null && targetNode.right == null) {
                    //当树只有1 10的时候，10作为根节点，没有父节点，所以parent自然为空，加一层判断。如果是根节点（parent为空），且只有一个子树的条件下，直接让root指向左子节点
                    if (parent != null) {
                        if (parent.left != null && parent.left.value == targetNode.value) {
                            parent.left = targetNode.left;
                        }
                        if (parent.right != null && parent.right.value == targetNode.value) {
                            parent.right = targetNode.left;
                        }
                    } else {
                        //根节点且只有左子树，将根节点指向左子节点
                        root = targetNode.left;
                    }
                }
                //待删除结点只有右子节点
                if (targetNode.right != null && targetNode.left == null) {
                    if (parent != null) {
                        if (parent.left != null && parent.left.value == targetNode.value) {
                            parent.left = targetNode.right;
                        }
                        if (parent.right != null && parent.right.value == targetNode.value) {
                            parent.right = targetNode.right;
                        }
                    } else {
                        //根节点且只有右子树，将根节点指向右子节点
                        root = targetNode.right;
                    }
                }

            }
        }
    }

}

class BinarySearchTreeNode {
    int value;
    BinarySearchTreeNode left;
    BinarySearchTreeNode right;

    public BinarySearchTreeNode(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public BinarySearchTreeNode getLeft() {
        return left;
    }

    public void setLeft(BinarySearchTreeNode left) {
        this.left = left;
    }

    public BinarySearchTreeNode getRight() {
        return right;
    }

    public void setRight(BinarySearchTreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "BinarySearchTreeNode{" +
                "value=" + value +
                '}';
    }

    public void add(BinarySearchTreeNode node) {
        if (node == null) {
            return;
        }
        //插入节点小于当前节点，就在左子树找插入位置，如果左子树没有节点，就直接插入，如果有，就递归的插入
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        }
        //插入节点大于当前节点，就在右子树找插入位置，如果右子树没有节点，就直接插入，如果有，就递归的插入
        if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }


    //搜寻待删除结点
    public BinarySearchTreeNode search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            //在左子树递归的查找待删除结点
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            //在右子树递归的查找待删除结点
            return this.right.search(value);
        }
    }

    //搜寻待删除结点的父节点
    public BinarySearchTreeNode searchParent(int value) {
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            //如果查找的值小于当前的值，且左子节点不为空
            if (this.left != null && value < this.value) {
                return this.left.searchParent(value);//向左递归查找
            } else if (this.right != null && value >= this.value) {
                return this.right.searchParent(value);//向右递归查找
            } else {
                return null;//value依然小于当前BST的value,但是已经没有左或右节点去递归，即找不到待删除元素
            }
        }
    }

    /**
     * 使用中序遍历方式遍历BST可以得到一个有序的序列
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}
